package cn.crm.web.controller.business;

import cn.crm.common.annotation.Log;
import cn.crm.common.core.controller.BaseController;
import cn.crm.common.core.domain.AjaxResult;
import cn.crm.common.core.page.TableDataInfo;
import cn.crm.common.enums.BusinessType;
import cn.crm.common.utils.poi.ExcelUtil;
import cn.crm.system.domain.pojo.entity.BusinessAndContactsUpdate;
import cn.crm.system.domain.pojo.vo.BusinessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import cn.crm.system.domain.pojo.dto.BusinessDto;
import cn.crm.system.service.impl.BusinessServiceImpl;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static cn.crm.common.utils.PageUtils.startPage;

@Slf4j
@RestController
@RequestMapping("/business")
@Api(tags = "业务模块")
public class BusinessController extends BaseController {

    @Autowired
    private BusinessServiceImpl businessService;


    public BusinessController() {
        log.debug("创建控制器对象：BusinessController");
    }

    @PreAuthorize("@ss.hasPermi('system:business:add')")
    @PostMapping("/add")
    @ApiOperation("添加客户资料")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    public AjaxResult  addBusiness(@RequestBody BusinessDto businessDto) {
        log.debug("开始处理添加客户资料的接口");
        int insertbusiness = businessService.insertbusiness(businessDto);
        if (insertbusiness < 1){
            return AjaxResult.error("添加公司失败");
        }
        return AjaxResult.success("添加成功");
    }

    @GetMapping("/list")
    @ApiOperation("查询客户资料")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @Log(title = "客户管理", businessType = BusinessType.OTHER)
    public TableDataInfo GetBusinesslist(BusinessDto businessDto) {
        log.debug("开始处理查询客户资料的接口");
        startPage();
        List<BusinessVo> businessVos = businessService.selectbusinessList(businessDto);
//        return null;
       return  getDataTable(businessVos);
    }



    @GetMapping("/count")
    @ApiOperation("查询客户总数")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @Log(title = "客户管理", businessType = BusinessType.OTHER)
    public  AjaxResult GetBusinesscount() {

        Long selectcount = businessService.selectcount();
//        return null;
        return  AjaxResult.success(selectcount);
    }


    @GetMapping("/percentage")
    @ApiOperation("查询客户每月百分比总数")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @Log(title = "客户管理", businessType = BusinessType.OTHER)
    public  AjaxResult GetBusinesspercentage() {

        Long selectcount = businessService.selectpercentage();
//        return null;
        return  AjaxResult.success(selectcount);
    }




    @GetMapping("/listcustomerName")
    @ApiOperation("查询客户名称资料")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @Log(title = "客户管理", businessType = BusinessType.OTHER)
    public TableDataInfo GetBusinesscustomerNamelist(BusinessDto businessDto) {
        log.debug("开始处理查询客户资料的接口");
        List<BusinessVo> businessVos = businessService.selectbusinessList(businessDto);
        return  getDataTable(businessVos);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据客户编号查询客户资料")
    @PreAuthorize("@ss.hasPermi('system:business:query')")
    @Log(title = "客户管理", businessType = BusinessType.OTHER)
     public AjaxResult getInfo(@PathVariable Long id){
        log.debug("开始通过id查询客户资料的接口");
        BusinessVo selectbusinessbyid = businessService.selectbusinessbyid(id);
        return  success(selectbusinessbyid);
    }


    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    @ApiOperation("修改客户资料")
    @PreAuthorize("@ss.hasPermi('system:business:update')")
    public AjaxResult update( @RequestBody BusinessDto businessDto){
        log.debug("开始修改客户资料的接口");
        int updatebusiness = businessService.updatebusiness(businessDto);
        if (updatebusiness < 1){
            return AjaxResult.error("修改客户资料失败");
        }
        return AjaxResult.success("修改客户资料成功");
    }

    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @ApiOperation("删除客户资料")
    @PreAuthorize("@ss.hasPermi('system:business:delete')")
    public AjaxResult delete(@PathVariable Long[] id){
        log.debug("开始删除客户资料的接口");
        int Deletebusiness = businessService.deletebusiness(id);
        if (Deletebusiness < 1){
            return AjaxResult.error("删除客户资料失败");
        }

        return AjaxResult.success("删除客户资料成功");
    }


    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:business:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response,BusinessDto businessDto)
    {
        List<BusinessVo> businessVos = businessService.selectbusinessList(businessDto);
        ExcelUtil<BusinessVo> util = new ExcelUtil<BusinessVo>(BusinessVo.class);
        util.exportExcel(response, businessVos, "客户数据");
    }

    @GetMapping("/listbusinessAndcontacts")
    @ApiOperation("查询出全部客户名称已经联系人已经案件资料")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @Log(title = "客户管理", businessType = BusinessType.OTHER)
    public TableDataInfo GetBusinessAndcontactsAndcaseList() {
        log.debug("开始处理查询客户已经联系人以及案件资料的接口");
        List<BusinessVo> businessVo = businessService.selectbusinessListAndcast();
        return  getDataTable(businessVo);
    }


    //下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BusinessAndContactsUpdate> util = new ExcelUtil<BusinessAndContactsUpdate>(BusinessAndContactsUpdate.class);
        util.importTemplateExcel(response, "参数数据");
    }

    //上传
    @Log(title = "客户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:business:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport
                            ) throws Exception
    {
        ExcelUtil<BusinessAndContactsUpdate> util = new ExcelUtil<BusinessAndContactsUpdate>(BusinessAndContactsUpdate.class);
        List<BusinessAndContactsUpdate> SpecificationList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String s = businessService.importBusinessAndcontactsAndcase(SpecificationList, updateSupport, operName);
        return success(s);
    }
    //查询台湾内销人员
    @GetMapping("/listbusinessListTaiwan")
    @ApiOperation("查询台湾内销人员")
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @Log(title = "客户管理", businessType = BusinessType.OTHER)
    public TableDataInfo GetBusinessListTaiwan() {
        log.debug("开始处理查询台湾内销人员接口");
        List<BusinessVo> businessVos = businessService.selectbusinessListTaiwan();
        return  getDataTable(businessVos);
    }



}
